/*
 *  msnobj.c - Msn Object Functions
 *  Copyright (c) 2005 Derek Ingrouville (dergro@users.sourceforge.net)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <glib.h>
#include <stdlib.h>
#include <string.h>

#include "msnobj.h"

MsnObject *
msn_object_new ()
{
	MsnObject *obj = NULL;
	obj = g_new (MsnObject, 1);

	obj->creator = NULL;
	obj->size = 0;
	obj->type = MSN_OBJ_UNKNOWN;
	obj->location = NULL;
	obj->friendly = NULL;
	obj->sha1d = NULL;
	obj->sha1c = NULL;
	obj->objstr = NULL;

	msn_object_set_friendly(obj, "AAA=");

	return obj;
}

MsnObject *
msn_object_new_from_string (const char *str)
{
	MsnCommand *cmd = NULL;
	MsnObject *obj = NULL;
	int i = 0;

	g_return_val_if_fail (str != NULL, NULL);
	
	if (strncmp (str, "<msnobj ", 8))
		return NULL;

	obj = msn_object_new ();
	cmd = msn_command_from_string (str);

	if (cmd->param_count != 7) {
		g_warning ("Invalid msn object being processed\n");
		g_free (cmd);
		return NULL;
	}

	for (i = 0; i < 7; i++) {
		char *start = NULL;
		char *end = NULL;
		char *value = NULL;

		start = strstr (cmd->params [i], "=");
		start += 2;
		end = strstr (start, "\"");
		value = g_strndup (start, (gsize) (end - start));

		switch (i) {
			case 0: {
				msn_object_set_creator (obj, value);
				break;
			}
			case 1: {
				obj->size = atoi (value);
				break;
			}
			case 2: {
				obj->type = (MsnObjType) atoi (value);
				break;
			}
			case 3: {
				msn_object_set_location (obj, value);
				break;
			}
			case 4: {
				msn_object_set_friendly (obj, value);
				break;
			}
			case 5: {
				msn_object_set_sha1d (obj, value);
				break;
			}
			case 6: {
				msn_object_set_sha1c (obj, value);
				break;
			}
			default: break;
		}

		g_free (value);
	}

	g_free (cmd);
	obj->objstr = g_strdup (str);

	return obj;
}

void
msn_object_free (MsnObject *obj)
{
	if (obj->creator != NULL)
		g_free (obj->creator);

	if (obj->location != NULL)
		g_free (obj->location);

	if (obj->friendly != NULL)
		g_free (obj->friendly);

	if (obj->sha1d != NULL)
		g_free (obj->sha1d);

	if (obj->sha1c != NULL)
		g_free (obj->sha1c);
	
	if (obj->objstr != NULL)
		g_free (obj->objstr);

	g_free (obj);
}

char *
msn_object_to_string (MsnObject *obj)
{
	char *str = NULL;

	g_return_val_if_fail (obj != NULL, NULL);
	g_return_val_if_fail (obj->creator != NULL, NULL);
	g_return_val_if_fail (obj->size > 0, NULL);
	g_return_val_if_fail (obj->type != MSN_OBJ_UNKNOWN, NULL);
	g_return_val_if_fail (obj->location != NULL, NULL);
	g_return_val_if_fail (obj->sha1d != NULL, NULL);
	g_return_val_if_fail (obj->sha1c != NULL, NULL);

	str = g_strdup_printf("<msnobj Creator=\"%s\" Size=\"%i\" Type=\"%i\" "
						  "Location=\"%s\" Friendly=\"%s\" SHA1D=\"%s\" "
						  "SHA1C=\"%s\"/>",
						  obj->creator,
						  obj->size,
						  obj->type,
						  obj->location,
						  obj->friendly,
						  obj->sha1d,
						  obj->sha1c);


	return str;
}

void
msn_object_set_creator (MsnObject *obj, char *creator)
{
	g_return_if_fail (obj != NULL);
	g_return_if_fail (creator != NULL);

	if (obj->creator != NULL)
		g_free (obj->creator);

	obj->creator = g_strdup (creator);
}

void
msn_object_set_location (MsnObject *obj, char *location)
{
	g_return_if_fail (obj != NULL);
	g_return_if_fail (location != NULL);

	if (obj->location != NULL)
		g_free (obj->location);

	obj->location = g_strdup (location);
}

void
msn_object_set_friendly (MsnObject *obj, char *friendly)
{
	g_return_if_fail (obj != NULL);
	g_return_if_fail (friendly != NULL);

	if (obj->friendly != NULL)
		g_free (obj->friendly);

	obj->friendly = g_strdup (friendly);
}

void
msn_object_set_sha1d (MsnObject *obj, char *sha1d)
{
	g_return_if_fail (obj != NULL);
	g_return_if_fail (sha1d != NULL);

	if (obj->sha1d != NULL)
		g_free (obj->sha1d);

	obj->sha1d = g_strdup (sha1d);
}

void
msn_object_set_sha1c (MsnObject *obj, char *sha1c)
{
	g_return_if_fail (obj != NULL);
	g_return_if_fail (sha1c != NULL);

	if (obj->sha1c != NULL)
		g_free (obj->sha1c);

	obj->sha1c = g_strdup (sha1c);
}
